﻿using System;
using System.Collections;
using System.Windows.Forms;

namespace HYFamilyLibManager.FamilyLibModule
{
	public class ListViewSorter : IComparer
	{
		public ListViewSorter()
		{
			this.sortColumn = 0;
			this.sortOrder = SortOrder.None;
			this.comparer = Comparer.Default;
		}

		public int SortColumn
		{
			get
			{
				return this.sortColumn;
			}
			set
			{
				this.sortColumn = value;
			}
		}

		public SortOrder SortOrder
		{
			get
			{
				return this.sortOrder;
			}
			set
			{
				this.sortOrder = value;
			}
		}

		public int Compare(object x, object y)
		{
			ListViewItem listViewItem = (ListViewItem)x;
			ListViewItem listViewItem2 = (ListViewItem)y;
			ListViewItem.ListViewSubItem listViewSubItem = listViewItem.SubItems[this.sortColumn];
			ListViewItem.ListViewSubItem listViewSubItem2 = listViewItem2.SubItems[this.sortColumn];
			int num = this.comparer.Compare(listViewSubItem.Text, listViewSubItem2.Text);
			if (!string.IsNullOrEmpty(listViewSubItem.Text) && !string.IsNullOrEmpty(listViewSubItem2.Text))
			{
				if (listViewSubItem.Tag is int && listViewSubItem2.Tag is int)
				{
					num = this.comparer.Compare(Convert.ToInt64(listViewSubItem.Text), Convert.ToInt64(listViewSubItem2.Text));
				}
				else if (listViewSubItem.Tag is long && listViewSubItem2.Tag is long)
				{
					num = this.comparer.Compare(Convert.ToInt64(listViewSubItem.Text), Convert.ToInt64(listViewSubItem2.Text));
				}
				else if (listViewSubItem.Tag is DateTime && listViewSubItem2.Tag is DateTime)
				{
					num = this.comparer.Compare(Convert.ToDateTime(listViewSubItem.Text), Convert.ToDateTime(listViewSubItem2.Text));
				}
			}
			if (this.SortOrder == SortOrder.Ascending)
			{
				return num;
			}
			if (this.SortOrder == SortOrder.Descending)
			{
				return -num;
			}
			return 0;
		}

		private Comparer comparer;

		private int sortColumn;

		private SortOrder sortOrder;
	}
}
